package protocol.JT905_2014;

import java.util.HashMap;

public class Handle905_2014JT02 implements IHandle905_2014JT {
    @Override
    public String handle(JT_905_2014Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle0200(cmd);
            case 0x01:
                return handle0201(cmd);
            case 0x03:
                return handle0203(cmd);
            default:
                return "905:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle0203(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:据盲区补传");
        builder.append("\r\n");
        do {
            JT905_2014Utils.handle0200Base(cmd,builder);
            builder.append("\r\n");
            builder.append("\r\n");
        }while (cmd.leftSize() > 0);

        return builder.toString();
    }
    private String handle0200(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:位置信息汇报");
        builder.append("\r\n");
        int warn = cmd.readInt(4);
        builder.append("报警:"+warn);
        builder.append("\r\n");
        builder.append("    紧急报警："+(warn&0x01));
        builder.append("\r\n");
        builder.append("    预警："+(warn>>1&0x01));
        builder.append("\r\n");
        builder.append("    卫星定位模块发生故障："+(warn>>2&0x01));
        builder.append("\r\n");
        builder.append("    卫星定位天线未接或被剪断："+(warn>>3&0x01));
        builder.append("\r\n");
        builder.append("    卫星定位天线短路："+(warn>>4&0x01));
        builder.append("\r\n");
        builder.append("    终端主电源欠压："+(warn>>5&0x01));
        builder.append("\r\n");
        builder.append("    终端主电源掉电："+(warn>>6&0x01));
        builder.append("\r\n");
        builder.append("    终端显示ISU故障："+(warn>>7&0x01));
        builder.append("\r\n");
        builder.append("    语音合成（TTS）模块故障："+(warn>>8&0x01));
        builder.append("\r\n");
        builder.append("    摄像头故障："+(warn>>9&0x01));
        builder.append("\r\n");
        builder.append("    计程计价装置故障："+(warn>>10&0x01));
        builder.append("\r\n");
        builder.append("    服务评价器故障："+(warn>>11&0x01));
        builder.append("\r\n");
        builder.append("    LED广告屏故障："+(warn>>12&0x01));
        builder.append("\r\n");
        builder.append("    液晶（终端）显示屏故障："+(warn>>13&0x01));
        builder.append("\r\n");
        builder.append("    安全访问模块故障："+(warn>>14&0x01));
        builder.append("\r\n");
        builder.append("    LED顶灯故障："+(warn>>15&0x01));
        builder.append("\r\n");
        builder.append("    超速报警："+(warn>>16&0x01));
        builder.append("\r\n");
        builder.append("    连续驾驶超时："+(warn>>17&0x01));
        builder.append("\r\n");
        builder.append("    当天累计驾驶超时："+(warn>>18&0x01));
        builder.append("\r\n");
        builder.append("    超时停车："+(warn>>19&0x01));
        builder.append("\r\n");
        builder.append("    进出区域/路线："+(warn>>20&0x01));
        builder.append("\r\n");
        builder.append("    路段行驶时间不足/过长："+(warn>>21&0x01));
        builder.append("\r\n");
        builder.append("    禁行路段行驶："+(warn>>22&0x01));
        builder.append("\r\n");
        builder.append("    车速传感器故障："+(warn>>23&0x01));
        builder.append("\r\n");
        builder.append("    车辆非法点火："+(warn>>24&0x01));
        builder.append("\r\n");
        builder.append("    车辆非法位移："+(warn>>25&0x01));
        builder.append("\r\n");
        builder.append("    智能终端存储异常："+(warn>>26&0x01));
        builder.append("\r\n");
        builder.append("    录音设备故障："+(warn>>27&0x01));
        builder.append("\r\n");
        builder.append("    计价器实时时间超过规定的误差范围："+(warn>>28&0x01));
        builder.append("\r\n");

        int status = cmd.readInt(4);
        builder.append("状态："+status);
        builder.append("    卫星定位状态（0为定位）："+(status&0x01));
        builder.append("\r\n");
        builder.append("    南北纬（0为北）："+(status>>1&0x01));
        builder.append("\r\n");
        builder.append("    东西经（0为东）："+(status>>2&0x01));
        builder.append("\r\n");
        builder.append("    停运状态（1为停运）："+(status>>3&0x01));
        builder.append("\r\n");
        builder.append("    预约状态（0为未预约）："+(status>>4&0x01));
        builder.append("\r\n");
        builder.append("    空转重（0为默认）："+(status>>5&0x01));
        builder.append("\r\n");
        builder.append("    重转空（0为默认）："+(status>>6&0x01));
        builder.append("\r\n");
        builder.append("    预留："+(status>>7&0x01));
        builder.append("\r\n");
        builder.append("    ACC(0为关)："+(status>>8&0x01));
        builder.append("\r\n");
        builder.append("    空重车（0为空）："+(status>>9&0x01));
        builder.append("\r\n");
        builder.append("    车辆油路断开（0为正常）："+(status>>10&0x01));
        builder.append("\r\n");
        builder.append("    车辆电路断开（0为正常）："+(status>>11&0x01));
        builder.append("\r\n");
        builder.append("    车门加锁（0为解锁）："+(status>>12&0x01));
        builder.append("\r\n");
        builder.append("    车辆锁定（0为未锁定）："+(status>>13&0x01));
        builder.append("\r\n");
        builder.append("    已达到限制营运次数/时间（0为未达到）："+(status>>14&0x01));
        builder.append("\r\n");

        builder.append("纬度："+cmd.readInt(4));
        builder.append("\r\n");
        builder.append("经度："+cmd.readInt(4));
        builder.append("\r\n");
        builder.append("速度："+cmd.readInt(2));
        builder.append("\r\n");
        builder.append("方向（1~179，刻度为2，正北为0）："+cmd.readInt(1));
        builder.append("\r\n");
        builder.append("时间："+cmd.readBCD(6));
        builder.append("\r\n");

        if (cmd.leftSize() > 2){
            while (cmd.leftSize() > 2){
                int id = cmd.readInt(1);
                int size = cmd.readInt(1);
                switch (id){
                    case 0x01:
                        builder.append("里程："+cmd.readInt(4));
                        builder.append("\r\n");
                        break;
                    case 0x02:
                        builder.append("油量："+cmd.readInt(2));
                        builder.append("\r\n");
                        break;
                    case 0x03:
                        builder.append("海拔高度："+cmd.readInt(2));
                        builder.append("\r\n");
                        break;
                    case 0x11: {
                        int type = cmd.readInt(1);
                        HashMap<Integer, String> list = new HashMap<>();
                        list.put(0, "无特定位置");
                        list.put(1, "圆形区域");
                        list.put(2, "矩形区域");
                        list.put(3, "多边形区域");
                        list.put(4, "路段");
                        builder.append("超速报警附加信息：" + type);
                        builder.append("    位置类型(" + type + "):" + list.getOrDefault(type, "未定义"));
                        builder.append("\r\n");
                        builder.append("    区域或路段ID:" + cmd.readDWord());
                        builder.append("\r\n");

                        if (size != 5) {
                            cmd.moveIndex(size - 5);
                        }
                        break;
                    }
                    case 0x12: {
                        int type = cmd.readInt(1);
                        HashMap<Integer, String> list = new HashMap<>();
                        list.put(1, "圆形区域");
                        list.put(2, "矩形区域");
                        list.put(3, "多边形区域");
                        list.put(4, "路段");
                        builder.append("进出区域/路线报警附加信息：" + type);
                        builder.append("    位置类型(" + type + "):" + list.getOrDefault(type, "未定义"));
                        builder.append("\r\n");
                        builder.append("    区域或路段ID:" + cmd.readDWord());
                        builder.append("\r\n");
                        list.clear();
                        list.put(0, "进");
                        list.put(1, "出");
                        builder.append("    方向:" +list.getOrDefault(cmd.readInt(1),"未定义"));
                        builder.append("\r\n");

                        if (size != 6) {
                            cmd.moveIndex(size - 6);
                        }
                        break;
                    }
                    case 0x13:{
                        builder.append("路段行驶时间不足/过长报警附加信息：");
                        builder.append("\r\n");
                        builder.append("    路段ID:" + cmd.readDWord());
                        builder.append("\r\n");
                        builder.append("    路段行驶时间:" + cmd.readWord());
                        builder.append("\r\n");

                        int type = cmd.readInt(1);
                        HashMap<Integer, String> list = new HashMap<>();
                        list.put(0, "不足");
                        list.put(1, "过长");
                        builder.append("    结果("+type+"):" +list.getOrDefault(cmd.readInt(1),"未定义"));
                        builder.append("\r\n");
                        if (size != 7) {
                            cmd.moveIndex(size - 7);
                        }
                        break;
                    }

                    case 0x14:
                        builder.append("禁行路段行驶报警附加信息："+cmd.readBCD(size));
                        builder.append("\r\n");
                        break;
                }
            }
        }

        return builder.toString();
    }


    private String handle0201(JT_905_2014Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:位置信息查询应答");
        builder.append("\r\n");
        builder.append("应答流水号："+cmd.readInt(2));
        builder.append("\r\n");
        int warn = cmd.readInt(4);
        builder.append("报警:"+warn);
        builder.append("\r\n");
        builder.append("    紧急报警："+(warn&0x01));
        builder.append("\r\n");
        builder.append("    预警："+(warn>>1&0x01));
        builder.append("\r\n");
        builder.append("    卫星定位模块发生故障："+(warn>>2&0x01));
        builder.append("\r\n");
        builder.append("    卫星定位天线未接或被剪断："+(warn>>3&0x01));
        builder.append("\r\n");
        builder.append("    卫星定位天线短路："+(warn>>4&0x01));
        builder.append("\r\n");
        builder.append("    终端主电源欠压："+(warn>>5&0x01));
        builder.append("\r\n");
        builder.append("    终端主电源掉电："+(warn>>6&0x01));
        builder.append("\r\n");
        builder.append("    终端显示ISU故障："+(warn>>7&0x01));
        builder.append("\r\n");
        builder.append("    语音合成（TTS）模块故障："+(warn>>8&0x01));
        builder.append("\r\n");
        builder.append("    摄像头故障："+(warn>>9&0x01));
        builder.append("\r\n");
        builder.append("    计程计价装置故障："+(warn>>10&0x01));
        builder.append("\r\n");
        builder.append("    服务评价器故障："+(warn>>11&0x01));
        builder.append("\r\n");
        builder.append("    LED广告屏故障："+(warn>>12&0x01));
        builder.append("\r\n");
        builder.append("    液晶（终端）显示屏故障："+(warn>>13&0x01));
        builder.append("\r\n");
        builder.append("    安全访问模块故障："+(warn>>14&0x01));
        builder.append("\r\n");
        builder.append("    LED顶灯故障："+(warn>>15&0x01));
        builder.append("\r\n");
        builder.append("    超速报警："+(warn>>16&0x01));
        builder.append("\r\n");
        builder.append("    连续驾驶超时："+(warn>>17&0x01));
        builder.append("\r\n");
        builder.append("    当天累计驾驶超时："+(warn>>18&0x01));
        builder.append("\r\n");
        builder.append("    超时停车："+(warn>>19&0x01));
        builder.append("\r\n");
        builder.append("    进出区域/路线："+(warn>>20&0x01));
        builder.append("\r\n");
        builder.append("    路段行驶时间不足/过长："+(warn>>21&0x01));
        builder.append("\r\n");
        builder.append("    禁行路段行驶："+(warn>>22&0x01));
        builder.append("\r\n");
        builder.append("    车速传感器故障："+(warn>>23&0x01));
        builder.append("\r\n");
        builder.append("    车辆非法点火："+(warn>>24&0x01));
        builder.append("\r\n");
        builder.append("    车辆非法位移："+(warn>>25&0x01));
        builder.append("\r\n");
        builder.append("    智能终端存储异常："+(warn>>26&0x01));
        builder.append("\r\n");
        builder.append("    录音设备故障："+(warn>>27&0x01));
        builder.append("\r\n");
        builder.append("    计价器实时时间超过规定的误差范围："+(warn>>28&0x01));
        builder.append("\r\n");

        int status = cmd.readInt(4);
        builder.append("状态："+status);
        builder.append("    卫星定位状态（0为定位）："+(status&0x01));
        builder.append("\r\n");
        builder.append("    南北纬（0为北）："+(status>>1&0x01));
        builder.append("\r\n");
        builder.append("    东西经（0为东）："+(status>>2&0x01));
        builder.append("\r\n");
        builder.append("    停运状态（1为停运）："+(status>>3&0x01));
        builder.append("\r\n");
        builder.append("    预约状态（0为未预约）："+(status>>4&0x01));
        builder.append("\r\n");
        builder.append("    空转重（0为默认）："+(status>>5&0x01));
        builder.append("\r\n");
        builder.append("    重转空（0为默认）："+(status>>6&0x01));
        builder.append("\r\n");
        builder.append("    预留："+(status>>7&0x01));
        builder.append("\r\n");
        builder.append("    ACC(0为关)："+(status>>8&0x01));
        builder.append("\r\n");
        builder.append("    空重车（0为空）："+(status>>9&0x01));
        builder.append("\r\n");
        builder.append("    车辆油路断开（0为正常）："+(status>>10&0x01));
        builder.append("\r\n");
        builder.append("    车辆电路断开（0为正常）："+(status>>11&0x01));
        builder.append("\r\n");
        builder.append("    车门加锁（0为解锁）："+(status>>12&0x01));
        builder.append("\r\n");
        builder.append("    车辆锁定（0为未锁定）："+(status>>13&0x01));
        builder.append("\r\n");
        builder.append("    已达到限制营运次数/时间（0为未达到）："+(status>>14&0x01));
        builder.append("\r\n");

        builder.append("纬度："+cmd.readInt(4));
        builder.append("\r\n");
        builder.append("经度："+cmd.readInt(4));
        builder.append("\r\n");
        builder.append("速度："+cmd.readInt(2));
        builder.append("\r\n");
        builder.append("方向（1~179，刻度为2，正北为0）："+cmd.readInt(1));
        builder.append("\r\n");
        builder.append("时间："+cmd.readBCD(6));
        builder.append("\r\n");

        if (cmd.leftSize() > 2) {
            while (cmd.leftSize() > 2) {
                int id = cmd.readInt(1);
                int size = cmd.readInt(1);
                switch (id) {
                    case 0x01:
                        builder.append("里程：" + cmd.readInt(4));
                        builder.append("\r\n");
                        break;
                    case 0x02:
                        builder.append("油量：" + cmd.readInt(2));
                        builder.append("\r\n");
                        break;
                    case 0x03:
                        builder.append("海拔高度：" + cmd.readInt(2));
                        builder.append("\r\n");
                        break;
                    case 0x11:
                        builder.append("超速报警附加信息：" + cmd.readBCD(size));
                        builder.append("\r\n");
                        break;
                    case 0x12:
                        builder.append("进出区域/路线报警附加信息：" + cmd.readBCD(size));
                        builder.append("\r\n");
                        break;
                    case 0x13:
                        builder.append("路段行驶时间不足/过长报警附加信息：" + cmd.readBCD(size));
                        builder.append("\r\n");
                        break;
                    case 0x14:
                        builder.append("禁行路段行驶报警附加信息：" + cmd.readBCD(size));
                        builder.append("\r\n");
                        break;
                }
            }
        }
        return builder.toString();
    }

}
